package xsdcl.window;

import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.DefaultComboBoxModel;

import xsdcl.TreeBuilder;
import xsdcl.TreeEdge;
import xsdcl.TreeEdgeUtils;
import xsdcl.TreeNode;
import xsdcl.TreeNodeUtils;
import xsdcl.NodeClassifier.EdgeClasses;
import xsdcl.NodeClassifier.NodeClasses;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class GeneralInfoWindow extends JFrame
{

	private JPanel contentPane;
	private JList listInfo;
	private JComboBox comboBox;
	private JScrollPane scrollPane;

	private NodeInfoWindow niw;
	private EdgeInfoWindow eiw;
	private JLabel lblCount;
	private boolean selectedNodeClass;

	private static GeneralInfoWindow instance;

	public static GeneralInfoWindow getInstance()
	{
		if (instance == null)
			instance = new GeneralInfoWindow();
		return instance;
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{
					// GeneralInfoWindow frame = new GeneralInfoWindow();
					// frame.setVisible(true);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * 
	 * @param infoWindow
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public GeneralInfoWindow()
	{
		this.niw = NodeInfoWindow.getInstance();
		setTitle("General Information");
		// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		setBounds(100, 100, 519, 389);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"), }, new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"), }));

		comboBox = new JComboBox();
		comboBox.addItemListener(new ItemListener()
		{
			public void itemStateChanged(ItemEvent arg0)
			{

				String item = (String) arg0.getItem();
				fillList(item);

			}
		});
		comboBox.setModel(new DefaultComboBoxModel(new String[] { "OC", "RN",
				"AN", "CA", "ERT", "OID", "OA", "RA", "DOC", "NULL", "IDD",
				"IRT" }));
		contentPane.add(comboBox, "2, 2, fill, default");

		lblCount = new JLabel("Count");
		contentPane.add(lblCount, "2, 4");

		scrollPane = new JScrollPane();
		contentPane.add(scrollPane, "2, 6, fill, fill");

		listInfo = new JList();
		listInfo.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{

				if (arg0.getClickCount() < 2)
					return;

				if (selectedNodeClass)
				{
					String item = (String) listInfo.getSelectedValue();
					TreeNode clickedNode = TreeNodeUtils.FindNodeByXPath(item);
					niw.displayInfo(clickedNode);
				}
				else
				{
					String item = (String) listInfo.getSelectedValue();
					TreeEdge clickEdge = TreeEdgeUtils.FindEdgeByName(item);
					if (eiw == null)
					{
						eiw = EdgeInfoWindow.getInstance();
					}
					eiw.setVisible(true);
					eiw.displayInfo(clickEdge);

				}
			}
		});
		listInfo.setModel(new AbstractListModel()
		{
			String[] values = new String[] {};

			public int getSize()
			{
				return values.length;
			}

			public Object getElementAt(int index)
			{
				return values[index];
			}
		});
		scrollPane.setViewportView(listInfo);

		fillList("OC");

	}

	private void fillList(String item)
	{
		try
		{
			NodeClasses nc = NodeClasses.valueOf(item);

			if (nc != null)
			{
				List<String> nodeNameList = new ArrayList<String>();
				for (int i = 0; i < TreeBuilder.getNodeCount(); i++)
				{
					TreeNode curNode = TreeNodeUtils.FindNodeByNo(i);
					if (curNode.getNodeClass().equals(nc))
					{
						nodeNameList.add(curNode.getXPath());
					}
				}
				lblCount.setText("Type: Node, Count: " + nodeNameList.size());
				listInfo.setModel(new DefaultComboBoxModel(nodeNameList
						.toArray()));
				selectedNodeClass = true;
			}
		} catch (IllegalArgumentException e)
		{
			EdgeClasses ec = EdgeClasses.valueOf(item);
			List<String> edgeNameList = new ArrayList<String>();
			List<TreeEdge> edgeList = TreeEdgeUtils.getEdgeList();
			if (!edgeList.isEmpty())
			{
				for (TreeEdge edge : TreeEdgeUtils.getEdgeList())
				{

					if (edge.getEdgeClass().equals(ec))
					{
						edgeNameList.add(edge.getName());
					}
				}
			}
			lblCount.setText("Type: Edge, Count: " + edgeNameList.size());
			listInfo.setModel(new DefaultComboBoxModel(edgeNameList.toArray()));
			selectedNodeClass = false;
		}

	}

}
